﻿#ifndef MOTORDRIVER_H
#define MOTORDRIVER_H

#include "../can/candevice.h"
#include <map>
using namespace std;

enum PDS_STA { QUICK_STOP = 0X00, SWITCH_ON = 0X06, DISABLE_OPERATION = 0X07, ENABLE_OPERATION = 0X0F };

enum NODE_INDEX {
    LEFT_NODE_ID  = 1,  //左轮伺服id
    RIGHT_NODE_ID = 2   //右轮伺服id
};

enum CAN_ID { CAN_ID_SEND = 0X600, CAN_ID_REC = 0X580 };

enum NMT_MODE {
    NMT_OPERATION_MODE = 0X01,
    NMT_STOP_MODE      = 0X02,
};

#pragma pack( push, 1 )
typedef struct data_encapsulation {
    uint8 cs : 8;
    uint16 od_index : 16;
    uint8 od_subindex : 8;
    uint val : 32;
} DATA_ENC;
#pragma pack( pop )

typedef union data_convert {
    DATA_ENC val;
    uchar    array[ 8 ];
} DATA_CONV;

// SDO读写及响应命令
typedef enum command_specifier {
    CS_WRITE_1     = 0X2F,
    CS_WRITE_2     = 0X2B,
    CS_WRITE_3     = 0X27,
    CS_WRITE_4     = 0X23,
    CS_READ        = 0x40,
    CS_READ_1      = 0x4F,
    CS_READ_2      = 0x4B,
    CS_READ_3      = 0x47,
    CS_READ_4      = 0x43,
    CS_RESPOND_OK  = 0x60,  //异常响应
    CS_RESPOND_ERR = 0x80   //异常响应
} CS;

typedef struct single_motor_feedback_para {
    double speed;
    int    pos;
    int    err;
    int    puls;

} SMotorFeedBackPara;

typedef struct double_motor_feedback_para {
    SMotorFeedBackPara lmfp;
    SMotorFeedBackPara rmfp;
} DMotorFeedBackPara;

class MotorDriver {
public:
    map< NODE_INDEX, char > wheel_direction;

    // typedef struct motor_const_para
    // {
    //     int pulse_per_round;
    //     double length_per_pulse;
    //     int left_direction;
    //     int right_directio;
    // }MotorConstPara;

    MotorDriver(){};
    virtual ~MotorDriver(){};

    virtual void DataFramesMotorInit(CanDataFrame *frames, uint8 &i) = 0;//frames数组大小定义要大于等于50
    virtual void DataFramesMotorSpeed(CanDataFrame *frames, uint8 &i, double speedl, double speedr) = 0;
    virtual void DataFramesMotorRequest(CanDataFrame *frames, uint8 &i) = 0;    
    virtual QString RecDataParse(CanDataFrame* frames, uint8 cnt) = 0;
    virtual DMotorFeedBackPara RetMotorFeedbackParam() = 0;
    virtual void DataFramesMotorEnable(CanDataFrame* frames, uint8 &i) = 0;   //左右伺服电机重新使能
    virtual void DataFramesMotorDisable(CanDataFrame* frames, uint8 &i) = 0;  //左右伺服电机停止失能
    virtual void DataFramesAccDecUpdate(CanDataFrame *frames, uint8 &i, double& acc, double& dec ) = 0;
    virtual void DataFramesStopMotor(CanDataFrame *frames, uint8 &i) = 0;//全部伺服电机设置停止模式
    virtual void DataFramesGetDriveErrInfo(CanDataFrame *frames, uint8 &i) = 0;//获取驱动的错误信息
    virtual void ResetWatchDog(CanDataFrame *frames, uint8 &i) = 0;
    virtual void AlarmClean(CanDataFrame* frames, uint8 &i)  = 0;//报警清除


protected:
    virtual QString DataParse(NODE_INDEX index, uint8 (&array)[8], SMotorFeedBackPara &para) = 0;

};

#endif // MOTORDRIVER_H
